<template>
    <div class="share-card" @click="handleClick">
        <div class="card-header">
            <div class="user-info">
                <van-image :src="share.user.avatar || '/default-avatar.png'" width="40" height="40" round fit="cover" />
                <div class="user-details">
                    <div class="username">{{ share.user.username }}</div>
                    <div class="user-meta">
                        <span class="user-level">{{ getUserLevel(share.user.level || 1) }}</span>
                        <span class="separator">•</span>
                        <span class="post-time">{{ formatTime(share.created_at || share.createdAt || '') }}</span>
                    </div>
                </div>
            </div>
            <div class="card-actions">
                <van-icon name="ellipsis" @click.stop="showMoreActions" />
            </div>
        </div>

        <div class="card-content">
            <h3 class="share-title">{{ share.title }}</h3>
            <p class="share-content">{{ share.content }}</p>

            <!-- 图片展示 -->
            <div class="share-images" v-if="share.images && share.images.length > 0">
                <van-image v-for="(image, index) in share.images.slice(0, 3)" :key="index" :src="image.url" width="100"
                    height="100" fit="cover" @click.stop="previewImages(index)" />
                <div v-if="share.images.length > 3" class="more-images" @click.stop="previewImages(0)">
                    +{{ share.images.length - 3 }}
                </div>
            </div>

            <!-- 标签 -->
            <div class="share-tags" v-if="share.tags && share.tags.length > 0">
                <van-tag v-for="tag in share.tags.slice(0, 3)" :key="tag" type="primary" plain>
                    {{ tag }}
                </van-tag>
                <span v-if="share.tags.length > 3" class="more-tags">
                    +{{ share.tags.length - 3 }}
                </span>
            </div>
        </div>

        <div class="card-footer">
            <div class="engagement-stats">
                <div class="stat-item" @click.stop="toggleLike">
                    <van-icon :name="(share.is_liked || share.isLiked) ? 'good-job' : 'good-job-o'"
                        :color="(share.is_liked || share.isLiked) ? '#ee0a24' : '#969799'" />
                    <span>{{ share.likes_count || share.likesCount || 0 }}</span>
                </div>
                <div class="stat-item" @click.stop="showComments">
                    <van-icon name="chat-o" />
                    <span>{{ share.comments_count || share.commentsCount || 0 }}</span>
                </div>
                <div class="stat-item" @click.stop="shareContent">
                    <van-icon name="share-o" />
                    <span>{{ share.shares_count || 0 }}</span>
                </div>
            </div>
            <div class="action-buttons">
                <van-button size="mini" @click.stop="toggleBookmark">
                    <van-icon :name="share.is_bookmarked ? 'star' : 'star-o'" />
                    {{ share.is_bookmarked ? 'Saved' : 'Save' }}
                </van-button>
            </div>
        </div>

        <!-- 更多操作弹窗 -->
        <van-popup v-model:show="showActionsPopup" position="bottom" round>
            <div class="actions-popup">
                <div class="popup-header">
                    <h3>More Actions</h3>
                </div>
                <div class="action-list">
                    <div class="action-item" @click="reportContent">
                        <van-icon name="warning-o" />
                        <span>Report</span>
                    </div>
                    <div class="action-item" @click="blockUser">
                        <van-icon name="cross" />
                        <span>Block User</span>
                    </div>
                    <div class="action-item" @click="copyLink">
                        <van-icon name="link-o" />
                        <span>Copy Link</span>
                    </div>
                </div>
            </div>
        </van-popup>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { showToast, showImagePreview } from 'vant'
import type { CommunityShare } from '@/api'

interface Props {
    share: CommunityShare
}

interface Emits {
    (e: 'click', share: CommunityShare): void
    (e: 'like', share: CommunityShare): void
    (e: 'bookmark', share: CommunityShare): void
    (e: 'comment', share: CommunityShare): void
    (e: 'share', share: CommunityShare): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

const showActionsPopup = ref(false)

const getUserLevel = (level: number) => {
    const levels = {
        1: 'Beginner',
        2: 'Intermediate',
        3: 'Advanced',
        4: 'Expert',
        5: 'Master'
    }
    return levels[level as keyof typeof levels] || 'Beginner'
}

const formatTime = (timestamp: string) => {
    const now = new Date()
    const time = new Date(timestamp)
    const diff = now.getTime() - time.getTime()

    const minutes = Math.floor(diff / (1000 * 60))
    const hours = Math.floor(diff / (1000 * 60 * 60))
    const days = Math.floor(diff / (1000 * 60 * 60 * 24))

    if (minutes < 60) {
        return `${minutes}m ago`
    } else if (hours < 24) {
        return `${hours}h ago`
    } else if (days < 7) {
        return `${days}d ago`
    } else {
        return time.toLocaleDateString()
    }
}

const handleClick = () => {
    emit('click', props.share)
}

const toggleLike = () => {
    emit('like', props.share)
}

const toggleBookmark = () => {
    emit('bookmark', props.share)
}

const showComments = () => {
    emit('comment', props.share)
}

const shareContent = () => {
    emit('share', props.share)
}

const previewImages = (startIndex: number) => {
    if (props.share.images && props.share.images.length > 0) {
        const images = props.share.images.map((img: any) => img.url)
        showImagePreview({
            images,
            startPosition: startIndex,
            closeable: true
        })
    }
}

const showMoreActions = () => {
    showActionsPopup.value = true
}

const reportContent = () => {
    showActionsPopup.value = false
    showToast('Content reported')
}

const blockUser = () => {
    showActionsPopup.value = false
    showToast('User blocked')
}

const copyLink = async () => {
    showActionsPopup.value = false
    try {
        await navigator.clipboard.writeText(`${window.location.origin}/community/share/${props.share.id}`)
        showToast('Link copied')
    } catch (error) {
        showToast('Failed to copy link')
    }
}
</script>

<style lang="scss" scoped>
.share-card {
    background: white;
    border-radius: 12px;
    padding: 16px;
    margin-bottom: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    cursor: pointer;
    transition: all 0.3s ease;

    &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    }

    &:active {
        transform: translateY(0);
    }

    .card-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 12px;

        .user-info {
            display: flex;
            gap: 12px;
            align-items: center;

            .user-details {
                .username {
                    font-size: 16px;
                    font-weight: 600;
                    color: #323233;
                    margin-bottom: 2px;
                }

                .user-meta {
                    display: flex;
                    align-items: center;
                    gap: 6px;
                    font-size: 12px;
                    color: #969799;

                    .user-level {
                        background: #f0f8ff;
                        color: #1989fa;
                        padding: 2px 6px;
                        border-radius: 8px;
                        font-weight: 500;
                    }

                    .separator {
                        color: #c8c9cc;
                    }
                }
            }
        }

        .card-actions {
            .van-icon {
                color: #969799;
                cursor: pointer;
                padding: 4px;

                &:hover {
                    color: #646566;
                }
            }
        }
    }

    .card-content {
        margin-bottom: 16px;

        .share-title {
            font-size: 18px;
            font-weight: 600;
            color: #323233;
            margin: 0 0 8px 0;
            line-height: 1.4;
        }

        .share-content {
            font-size: 15px;
            color: #646566;
            line-height: 1.6;
            margin: 0 0 12px 0;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            line-clamp: 3;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .share-images {
            display: flex;
            gap: 8px;
            margin-bottom: 12px;
            flex-wrap: wrap;

            .van-image {
                border-radius: 8px;
                cursor: pointer;
            }

            .more-images {
                width: 100px;
                height: 100px;
                background: #f7f8fa;
                border-radius: 8px;
                display: flex;
                align-items: center;
                justify-content: center;
                font-size: 14px;
                color: #646566;
                cursor: pointer;
                border: 1px dashed #c8c9cc;
            }
        }

        .share-tags {
            display: flex;
            gap: 6px;
            align-items: center;
            flex-wrap: wrap;

            .more-tags {
                font-size: 12px;
                color: #969799;
            }
        }
    }

    .card-footer {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-top: 12px;
        border-top: 1px solid #f5f5f5;

        .engagement-stats {
            display: flex;
            gap: 20px;

            .stat-item {
                display: flex;
                align-items: center;
                gap: 4px;
                font-size: 14px;
                color: #646566;
                cursor: pointer;
                padding: 4px;
                border-radius: 4px;
                transition: background-color 0.3s;

                &:hover {
                    background: #f7f8fa;
                }

                .van-icon {
                    font-size: 16px;
                }
            }
        }

        .action-buttons {
            .van-button {
                font-size: 12px;
            }
        }
    }
}

.actions-popup {
    padding: 20px;

    .popup-header {
        margin-bottom: 16px;

        h3 {
            font-size: 18px;
            color: #323233;
            margin: 0;
            font-weight: 600;
            text-align: center;
        }
    }

    .action-list {
        .action-item {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 16px;
            border-radius: 8px;
            cursor: pointer;
            transition: background-color 0.3s;

            &:hover {
                background: #f7f8fa;
            }

            .van-icon {
                color: #646566;
                font-size: 18px;
            }

            span {
                font-size: 16px;
                color: #323233;
            }
        }
    }
}
</style>